import { getSubjectList, geosList } from '@/api/user'
import { handleTree } from '@/utils'

const state = {
  subjectList: [],
  geosList: [],
  subjectMark: [
    { name: '临床', subject_mark: 'lczy|lczl' },
    { name: '中医', subject_mark: 'zyzy|zyzl' },
    { name: '中西医', subject_mark: 'zxyzy|zxyzl' },
    { name: '口腔', subject_mark: 'kqzy|kqzl' },
    { name: '乡村', subject_mark: 'xcqk' },
    { name: '中药师', subject_mark: 'zys' },
    { name: '西药师', subject_mark: 'xys' },
    { name: '护士', subject_mark: 'hszg' },
    { name: '临床主治', subject_mark: 'lcnk|lcwk|lcqk' },
    { name: '健康管理师', subject_mark: 'jkgls' }
  ],
  subjectValue: 2
}

const mutations = {
  SUBJECT_LIST: (state, subject) => {
    state.subjectList = subject
  },
  GEOS_LIST: (state, geosList) => {
    state.geosList = geosList
  },
  SET_SUBVAL: (state, subjectValue) => {
    state.subjectValue = subjectValue
  }
}

const actions = {

  subject({ commit }) {
    return new Promise((resolve, reject) => {
      getSubjectList().then(response => {
        // commit('SUBJECT_LIST', handleTree(response))
        commit('SUBJECT_LIST', response.data)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  geos({ commit }) {
    return new Promise((resolve, reject) => {
      geosList().then(response => {
        // commit('GEOS_LIST', handleTree(response.filter(b => (b.depth !== 3)), 'code'))
        commit('GEOS_LIST', response.data)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  setSubjectValue({ commit }, subjectValue) {
    commit('SET_SUBVAL', subjectValue)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
